package com.hpin.fansea.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.bean.GoodsCommentBean;
import com.hpin.fansea.widget.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * Created by Aaron on 15/10/22.
 */
public class GoodsCommentAdapter extends BaseAdapter {

    private Context context;
    public DisplayImageOptions options;
    private List<GoodsCommentBean.GoodsComRes> goodsComRes;

    public GoodsCommentAdapter(Context context, List<GoodsCommentBean.GoodsComRes> goodsComRes) {
        this.context = context;
        this.goodsComRes = goodsComRes;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_headpic_default)
                .showImageForEmptyUri(R.mipmap.icon_headpic_default)
                .showImageOnFail(R.mipmap.icon_headpic_default)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();
    }

    public void setDate(List<GoodsCommentBean.GoodsComRes> goodsComRes) {
        this.goodsComRes = goodsComRes;
    }

    @Override
    public int getCount() {
//        LogUtil.e("tag", "count==" + goodsComRes.size());

        return goodsComRes.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {

            convertView = View.inflate(context, R.layout.item_comment, null);
            viewHolder = new ViewHolder();
            viewHolder.item_commemt_pthone = (TextView) convertView.findViewById(R.id.item_commemt_pthone);
            viewHolder.item_commemt_avatar = (CircleImageView) convertView.findViewById(R.id.item_commemt_avatar);
            viewHolder.item_commemt_content = (TextView) convertView.findViewById(R.id.item_commemt_content);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (goodsComRes.get(position).user != null) {

            viewHolder.item_commemt_pthone.setText(goodsComRes.get(position).user.nickname);

            ImageLoader.getInstance().displayImage(goodsComRes.get(position).user.headPic,viewHolder.item_commemt_avatar, options);
        }
        viewHolder.item_commemt_content.setText(goodsComRes.get(position).content);
//        viewHolder.tv_name.setText(cityList.get(position).cityName);

        return convertView;
    }

    private class ViewHolder {

        public CircleImageView item_commemt_avatar;
        public TextView item_commemt_pthone;
        public TextView item_commemt_content;

    }

}
